const db = wx.cloud.database()
const collectionName = 'active'
export default {
  getAllActive: function ({
    pageNum,
    pageSize,
    content
  }) {
    return new Promise((resolve, reject) => {

      var skip = (pageNum - 1) * pageSize
      var matchObj = {

      }
      if (content) {
        matchObj.content = db.RegExp({
          regexp: content,
          options: 'im',
        })
      }
      db.collection('active').aggregate()
        .match(matchObj)
        .sort({
          createDate: -1
        })
        .skip(skip)

        .limit(pageSize)
        .end()
        .then(res => {
          resolve(res.list)
        })
    })
  },
  addActive: function (data) {
    return new Promise((resolve, reject) => {
      if (data.longitude) {
        data.location = new db.Geo.Point(data.longitude, data.latitude)
      }

      data.createDate = new Date()

      db.collection(collectionName).add({
          data: data
        })
        .then(res => {
          console.log(res)
          resolve(res._id)
        })
        .catch(console.error)
    })
  },

  updateActive: async function (data) {
    var id = data.id
    delete data.id
    delete data.openid

    if (data.longitude) {
      data.location = new db.Geo.Point(data.longitude, data.latitude)
    }

    data.createDate = new Date()

    var res = await db.collection(collectionName).doc(id).update({

      data: data
    })
    return res
  },

  deleteActive: async function (id) {
    var res = await db.collection(collectionName).doc(id).remove()
    return res
  },

  getActive: function (_id) {
    return new Promise((resovle, reject) => {
      wx.cloud.callFunction({
        name: 'activeApi',
        data: {
          methodName: 'getActive',
          activeId: _id
        }
      }).then(res => {
        resovle(res.result.list)
      })

    })
  },
  joinActive: function (data) {
    return new Promise((resolve, reject) => {
      db.collection('join').add({
        data: {
          openid: data.openid,
          avatarUrl: data.avatarUrl,
          nickname: data.nickname,
          activeId: data.activeId,
          createDate: new Date()
        }
      }).then(res => {
        resolve(res._id)
      })
    })
  },

  deleteJoin: async function (activeId) {
    var res = await db.collection('join').where({
      activeId: activeId
    }).remove()

    return res
  },

  getMyActive: function (param) {
    return new Promise((resolve, reject) => {
      param.methodName = 'getMyActive'
      wx.cloud.callFunction({
        name: 'activeApi',
        data: param

      }).then(res => {
        resolve(res.result.list)
      })
    })
  },
  getMyJoinActive: function (param) {

    return new Promise((resolve, reject) => {
      wx.cloud.callFunction({
        name: 'activeApi',
        data: {
          methodName: 'getMyJoinActive',
          pageNum: param.pageNum,
          pageSize: param.pageSize
        }

      }).then(res => {
        resolve(res.result.list)
      })
    })
  },
  getIsShow: function () {
    return new Promise((resolve, reject) => {
      db.collection('show').get().then(res => {

        resolve(res.data)

      })
    })
  },

  near: async function ({
    lng,
    lat,
    pageNum,
    pageSize,
    content
  }) {
    let skip = (pageNum - 1) * pageSize
    var matchObj = {

    }
    if (content) {
      matchObj.content = db.RegExp({
        regexp: content,
        options: 'im',
      })
    }
    let res = await db.collection(collectionName).aggregate()
      .geoNear({
        distanceField: 'distance', // 输出的每个记录中 distance 即是与给定点的距离
        spherical: true,
        near: db.Geo.Point(lng, lat),

        // query: {
        //   docType: 'geoNear',
        // },
        key: 'location', // 若只有 location 一个地理位置索引的字段，则不需填
        includeLocs: 'location', // 若只有 location 一个是地理位置，则不需填
      })
      .match(matchObj)
      .skip(skip)

      .limit(pageSize)
      .end()

    return res.list
  },

  geoWithin: async function ({
    lng,
    lat
  }) {

    let res = await db.collection(collectionName).where({
      location: db.command.geoWithin({
        centerSphere: [
          [lng, lat],
          100 / 6378.1,
        ]
      })
    }).get()

    return res.data
  }
}